// 处理nexus相关

// 获取nexus坐标
def getGAV(){
    // 获取build完的包名
    def packageName = sh returnStdout: true,script: "cd target;ls *jar"
    env.packageName = packageName - "\n"

    // 从pom.xml文件中读取信息
    def pom = readMavenPom file: 'pom.xml'
    println(pom)
    env.pomVersion = "${pom.version}"
    env.pomArtifact = "${pom.artifactId}"
    env.pomPackaging = "${pom.packaging}"
    env.pomGroupId = "${pom.groupId}"
    
    println("${pomGroupId}-${pomArtifact}-${pomArtifact}-${pomPackaging}")
						
}


// 通过mvn命令上传nexus
def MavenUpload(){
    def mvnHome = tool "M2"
    sh """
        cd target/
        ${mvnHome}/bin/mvn deploy:deploy-file -Dmaven.test.skip=true \
            -Dfile=${packageName} -DgroupId=${pomGroupId} \
            -DartifactId=${pomArtifact} -Dversion=${pomVersion} \
            -Dpackaging=${pomPackaging} -DrepositoryId=maven-hostd \
            -Durl=http://10.1.10.22:8081/repository/maven-hostd
    """
}

// 通过nexus插件上传nexus
def NexusPluginUpload(){
    nexusArtifactUploader 	artifacts: [[artifactId: "${pomArtifact}", 
                                        classifier: '', 
                                        file: "${filePath}", 
                                        type: "${pomPackaging}"]], 
                            credentialsId: 'nexus-admin-user',
                            groupId: "${pomGroupId}",
                            nexusUrl: '10.1.10.22:8081',
                            nexusVersion: 'nexus3',
                            protocol: 'http',
                            repository: "${repoName}",
                            version: "${pomVersion}"
}

// 制品库晋级
def ArtifactUpdate(updateType,artifactUrl){
    // 判断晋级策略
    if ("${updateType}" == "PRE->PRO"){
        // 获取需要晋级的包
        sh """
            [ -e updates ] && rm -rf updates
            mkdir updates && cd updates && wget ${artifactUrl}
        """
        println(artifactUrl)
        // 获取包的GAV信息（http://10.1.10.22:8081/repository/maven-hostd/com/mycompany/app/my-app/1.1/my-app-1.1.jar）
        artifactUrl = artifactUrl - "http://10.1.10.22:8081/repository/maven-hostd/"
        artifactUrl = artifactUrl.split("/").toList()
        println(artifactUrl)

        env.packageName = artifactUrl[-1]
        env.pomVersion = artifactUrl[-2]
        env.pomArtifact = artifactUrl[-3]
        pomPackaging = artifactUrl[-1].toString().split("\\.").toList()[-1]
        println(pomPackaging)
        env.pomPackaging = pomPackaging[-1]
        temp = "/${pomArtifact}/${pomVersion}/${packageName}"
        env.pomGroupId = artifactUrl[0..2].join(".")
        
        println("##${pomGroupId}-##${pomArtifact}-##${pomVersion}-##${pomPackaging}")

        // 
        env.repoName = "maven-release"
        env.filePath = "updates/${packageName}"
        NexusPluginUpload()
    }
}


// 主函数
def main(uploadType){
    getGAV()
    if ("${uploadType}" == "maven"){
        MavenUpload()
    }else if ("${uploadType}" == "nexusPlg"){
        env.repoName = "maven-hostd"
        env.filePath = "target/${packageName}"
        NexusPluginUpload()
    }
}